﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Xml.Linq;

public partial class admin_Users_List : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      LoadRole();
      ShowData();
    }
  }
  UserBLL userData = new UserBLL();
  CommentBLL commentData = new CommentBLL();
  private void LoadRole()
  {
    dropRole.Items.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.XML");
    var xRole = xElement.Descendants("Roles").Descendants("Role");
    foreach (XElement item in xRole)
    {
      dropRole.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));
    }  
    ListItem itemNull = new ListItem("Tất cả", "-1");
    dropRole.Items.Insert(0, itemNull);
  }
  private void ShowData()
  {
    int roleId = Convert.ToInt32(dropRole.SelectedValue);
    int PageRow = Convert.ToInt32(dropPageSize.SelectedValue);
    gridView.PageSize = PageRow;
    if (dropIsEnable.SelectedIndex == 0)
    {
      if (roleId != -1)
        gridView.DataSource = userData.GetByRoleId(roleId);
      else
        gridView.DataSource = userData.GetAll();
    }
    else
    {
      bool isEnable = true;
      if (dropIsEnable.SelectedValue == "2") isEnable = false;

      if (roleId != -1)
        gridView.DataSource = userData.GetByRoleId(roleId, isEnable);
      else
        gridView.DataSource = userData.GetAll(isEnable);
    }
    gridView.DataBind();

  }
  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      if (e.Row.RowIndex >= 0) //Bo qua dong dau
      {
        int id = Convert.ToInt32(e.Row.Cells[1].Text);

        bool isEnable = userData.GetById(id).isEnable;

        //Icon check
        Image img = new Image();
        foreach (Control item in e.Row.Cells[6].Controls)
        {
          if (item.GetType() == typeof(Image))
          {
            img = (Image)item;
            break;
          }
        }
        if (isEnable)
          img.ImageUrl = "~/Images/check.png";
        else
        {
          img.ImageUrl = "~/Images/an.png";

        }
        string serverPath = Server.MapPath("~/");
        RoleBLL roleData = new RoleBLL(serverPath);
        e.Row.Cells[5].Text = roleData.GetById(Convert.ToInt32(e.Row.Cells[5].Text)).Name;
      }
    }
    catch { }
  }
  protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    lbThongBao.Text = "";
    try
    {
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  }
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[1].Text);

      commentData.DeleteByUserId(id);
      userData.Delete(id);
      Myfunction.ThongBao(lbThongBao,"Tài khoản đã được xóa");

      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  private void Enable(bool isEnable)
  {
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          if (check.Checked)
          {
            int id = Convert.ToInt32(item.Cells[1].Text);
            userData.Enable(id, isEnable);
          }
          break;
        }
      }
    }
    ShowData();
  }
  protected void btnVisible_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    lbThongBao.Text = "";
    Enable(false);
  }
  protected void btnEnable_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    lbThongBao.Text = "";
    Enable(true);
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[0].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[1].Text);
              userData.Delete(id);
            }
            break;
          }
        }
      }
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void btnAdd_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/admin/Users/Details.aspx");
  }
  protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
    ShowData();
  }
}